<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cycleGenerator</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const cycleGenerator = function* (arr) {
            let i = 0;
            while (true) {
                yield arr[i % arr.length];
                i++;
            }
        };
        const binaryCycle = cycleGenerator([0, 1]);
        console.log(binaryCycle.next()); // { value: 0, done: false }
        console.log(binaryCycle.next()); // { value: 1, done: false }
        console.log(binaryCycle.next()); // { value: 0, done: false }
        console.log(binaryCycle.next()); // { value: 1, done: false }
    </script>
</body>

</html>